package com.community.backend.aspect;


import com.community.backend.annotation.AutoFill;
import com.community.backend.context.BaseContext;
import com.community.backend.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * 自定义切面，实现公共字段自动填充处理逻辑
 */
@Aspect
@Component
@Slf4j
public class AutoFillAspect {
    /**
     * 切入点
     */
    @Pointcut("execution(* com.community.backend.mapper.*.*(..)) && @annotation(com.community.backend.annotation.AutoFill)")
    public void autoFillPointCut(){}

    /**
     * 前置通知
     * @param joinPoint
     */
    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint){
        log.info("开始进行公共字段填充");
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);
        OperationType operationType = autoFill.value();
        Object[] args = joinPoint.getArgs();
        if (args == null || args.length == 0){
            return;
        }
        Object entity = args[0];
        LocalDateTime now = LocalDateTime.now();
        String currentId = BaseContext.getCurrentId();
        String currentName = BaseContext.getCurrentName();
        String currentRole = BaseContext.getCurrentRole();
        log.info("公共字段填充当前用户：{},{},{}", currentId, currentName, currentRole);
        if (operationType.equals(OperationType.INSERT)){
            try {
                Method setCreateTime = entity.getClass().getDeclaredMethod("setCreateTime", LocalDateTime.class);
                Method setUpdateTime = entity.getClass().getDeclaredMethod("setUpdateTime", LocalDateTime.class);
                Method setCreateId = entity.getClass().getDeclaredMethod("setCreateId", String.class);
                Method setUpdateId = entity.getClass().getDeclaredMethod("setUpdateId", String.class);
                Method setCreateName = entity.getClass().getDeclaredMethod("setCreateName", String.class);
                Method setUpdateName = entity.getClass().getDeclaredMethod("setUpdateName", String.class);
                Method setCreateRole = entity.getClass().getDeclaredMethod("setCreateRole", String.class);
                Method setUpdateRole = entity.getClass().getDeclaredMethod("setUpdateRole", String.class);
                //通过反射为对应的属性赋值
                setCreateTime.invoke(entity, now);
                setUpdateTime.invoke(entity, now);
                setCreateId.invoke(entity, currentId);
                setUpdateId.invoke(entity, currentId);
                setCreateName.invoke(entity, currentName);
                setUpdateName.invoke(entity, currentName);
                setCreateRole.invoke(entity, currentRole);
                setUpdateRole.invoke(entity, currentRole);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }else if (operationType.equals(OperationType.UPDATE)){
            try {
                Method setUpdateId = entity.getClass().getDeclaredMethod("setUpdateId", String.class);
                Method setUpdateName = entity.getClass().getDeclaredMethod("setUpdateName", String.class);
                Method setUpdateRole = entity.getClass().getDeclaredMethod("setUpdateRole", String.class);
                Method setUpdateTime = entity.getClass().getDeclaredMethod("setUpdateTime", LocalDateTime.class);

                //通过反射为对应的属性赋值
                log.info("更新时间：{}", now);
                setUpdateTime.invoke(entity, now);

                setUpdateId.invoke(entity, currentId);

                setUpdateName.invoke(entity, currentName);

                setUpdateRole.invoke(entity, currentRole);
            } catch (Exception e) {
                throw new RuntimeException(e);

            }
        }
    }
}
